﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ECNow.Data.Mysql;
using ECNow.Data;
using ECNow.Core.Data;
//using ECNow.Core.Web;
//using ECNow.Servcie.Product;
using ECNow.Servcie.Products;

// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace ECNow.Portal.Controllers
{
    public class ProductController : Controller
    {
        // GET: /<controller>/
        public IActionResult Index()
        {
            return View("Product_List");
        }

        public IActionResult ProductList()
        {
            return View("Product_List");
        }

        public IActionResult ProductNew()
        {
            return View("Product_New");
        }
        public IActionResult ProductEdit(int id)
        {
            ViewData["Id"] = id;
            return View();
        }

        public IActionResult ProductGetList(int pageindex = 0, int pagesize = 20)
        {
            var result = new ProductDac().GetList(pageindex, pagesize);
            return Json(result);
        }

        public IActionResult ProductGet(int id)
        {
            var result = new ProductDac().Get(id);
            return Json(result);
        }

        public IActionResult ProductSave(Product entity)
        {
            var service = new ProductService();

            MessageResult msg = new MessageResult();
            try
            {
                var result = new ProductDac().Insert(entity);

                if (result > 0)
                {
                    msg.Message = "操作成功";
                    msg.Code = "Success";
                }
                else
                {
                    msg.Message = "操作失败";
                    msg.Code = "Failed";
                }
            }
            catch
            {
                msg.Message = "系统异常";
                msg.Code = "Exception";
            }
            return Json(msg);
        }

        public IActionResult ProductEdit(Product entity)
        {
            MessageResult msg = new MessageResult();
            try
            {
                var result = new ProductDac().Update(entity);
                if (result > 0)
                {
                    msg.Message = "操作成功";
                    msg.Code = "Success";
                }
                else
                {
                    msg.Message = "操作失败";
                    msg.Code = "Failed";
                }
            }
            catch
            {
                msg.Message = "系统异常";
                msg.Code = "Exception";
            }
            return Json(msg);
        }

        public IActionResult ProductVoid(int id)
        {
            MessageResult msg = new MessageResult();
            try
            {
                var result = new ProductDac().Delete(id);
                if (result > 0)
                {
                    msg.Message = "操作成功";
                    msg.Code = "Success";
                }
                else
                {
                    msg.Message = "操作失败";
                    msg.Code = "Failed";
                }
            }
            catch
            {
                msg.Message = "系统异常";
                msg.Code = "Exception";
            }
            return Json(msg);
        }
    }
}
